import axios from "axios";
import { storageLocal } from "../storage";
export const getBimAccessToken = async () => {
  const res = await axios.post(`/bim/oauth2/token`, null, {
    headers: {
      Authorization: `Basic MDVSczY4UjF6Y1pzdmxQUllKN3E2bHZZWEJTcU16RlY6aVAyZjNHa1AyS0VvbHRSQzhRSHdBcDg0emt4azFVQWs=`
    }
  });
  const token = res.data.data.token;
  storageLocal.setItem("bim-token", token);
  return token;
};

export const uploadBimFile = async file => {
  const token = storageLocal.getItem("bim-token");

  const res = await axios({
    method: "PUT",
    url: `/bim/file/upload?name=${file.name}`,
    data: file.raw,
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "actet-stream"
    }
  });
  await translateFile(res.data.data);
  return res.data.data;
};

export const translateFile = async file => {
  const token = storageLocal.getItem("bim-token");
  const res = await axios({
    method: "PUT",
    url: `/bim/translate`,
    data: {
      priority: 2,
      source: {
        compressed: false,
        fileId: file.fileId,
        rootName: file.name
      },
      callback: "https://www.baodu.com"
    },
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json"
    }
  });
  return res.data.data;
};

export const getTranslateStatus = async fileId => {
  const token = storageLocal.getItem("bim-token");
  const res = await axios({
    method: "GET",
    url: `/bim/translate`,
    params: {
      fileId
    },
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json"
    }
  });
  return res.data.data;
};

export const getFileViewToken = async fileId => {
  const token = storageLocal.getItem("bim-token");
  const res = await axios({
    method: "GET",
    url: `/bim/view/token`,
    params: {
      fileId
    },
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json"
    }
  });
  return res.data.data;
};
